    <?php
    //数组
    echo "<h1>用名称代替数字索引</h1>";
    $products = array(
        array(
            'Code' => 'TIR',
            'Description' => 'Tires',
            'Price' => 100
        ),

        array(
            'Code' => 'OIL',
            'Description' => 'Oil',
            'Price' => 10
        ),

        array(
            'Code' => 'SPK',
            'Description' => 'Spark Plugs',
            'Price' => 4
        ),

    );

    //自定义一维数组打印
    function oda($value) {
        for ($i = 0; $i < 10; $i++) {
            echo $value[$i] . "<br />";
        }
    }

        //一维数组，有值的
    function odaw($value) {

    foreach ($value as $key => $value) {
        echo $key . "-" . $value . "<br />";
    }
}

    //自定义二维数组打印
    function tda($value)
    {
        echo "打印当前二维数组<br />";
        for ($row = 0; $row < 3; $row++) {
            for ($column = 0; $column < 3; $column++) {
                echo '|' . $value[$row][$column];
            }
            echo '|<br />';
        }
        echo "当前二维数组打印完毕<br />";
    };

    ?>
    
    <?php
    echo "<h1>创建数组</h1>";
    $products = array('Tires', 'Oil', 'Spark Plugs');
    $products = ['Tires', 'Oil', 'Spark Plugs'];

    //range函数
    $number = range(1, 10); //创建一个从1到10的数组
    $odds = range(1, 10, 2); //创建一个奇数数组
    $letters = range('a', 'z');


    echo $products[0] . "<br />";
    //$products[0] = 'Fuses';//改变数组的值
    //echo $products[0]."<br />";

    $products[3] = 'Fuses';
    echo $products[3] . "<br />";



    echo "<h1>for循环打印</h1>";
    for ($i = 0; $i < 3; $i++) {
        echo $products[$i] . "<br />";
    }


    echo "<h1>foreach循环打印</h1>";
    foreach ($products as $current) {
        echo $current . "<br />";
    }


    echo "<h1>初始化数组</h1>";
    $prices = array('Tires' => 100, 'Oil' => 10, 'Spark Plugs' => 4);

    echo "<h1>用变量名称和键访问数组</h1>";
    echo $prices['Tires'] . "<br />";

    echo "<h1>用foreach访问数组</h1>";
    foreach ($prices as $key => $value) {
        echo $key . "-" . $value . "<br />";
    }

    echo "<h1>函数打印二维数组</h1>";
    //$printer = function( $value, $key ) {echo "$key - $value <br />";};
    //array_walk($products, $printer);


    //echo "<h1>用each()结构访问数组(不推荐)</h1>";
    //while ( $element = echo ($prices) ) {
    //    echo $element['key']."-".$element['value'];
    //    echo "<br />";
    //}

    echo "<h1>从头循环数组</h1>";
    reset($prices); //将元素设为数组的开始


    //echo "<h1>用list()结构访问数组(不推荐each())</h1>";

    //while (list($product, $price) = each($prices)) {
    //   echo $product . "-" . $price . "<br />";
    //}


    echo "<h1>二维数组</h1>";
    $products = array(
        array('TIR', 'Tires', 100),
        array('OIL', 'Oil', 10),
        array('SPK', 'Spark Plugs', 4)
    );

    echo '|' . $products[0][0] . '|' . $products[0][1] . '|' . $products[0][2] . '|<br />';
    echo '|' . $products[1][0] . '|' . $products[1][1] . '|' . $products[1][2] . '|<br />';
    echo '|' . $products[2][0] . '|' . $products[2][1] . '|' . $products[2][2] . '|<br />';

    //echo $prices['TIR'] . "<br />@@@";

    echo "<h1>双重FOR循环打印</h1>";
    for ($row = 0; $row < 3; $row++) {
        for ($column = 0; $column < 3; $column++) {
            echo '|' . $products[$row][$column];
        }
        echo '|<br />';
    }



    echo "<h1>用名称代替数字索引</h1>";
    for ($row = 0; $row < 3; $row++) {
        echo '|' . $products[$row]['Code'] . '|' . $products[$row]['Description'] . '|' . $products[$row]['Price'] . '|<br />';
    }


    echo "<h1>三维数组</h1>";
    $categories = array(

        array(
            array('CAR_TIR', 'Tires', 100),
            array('CAR_OIL', 'Oil', 10),
            array('CAR_SPK', 'Spark Plugs', 4)
        ),

        array(
            array('VAN_TIR', 'Tires', 120),
            array('VAN_OIL', 'Oil', 12),
            array('VAN_SPK', 'Spark Plugs', 5)
        ),

        array(
            array('TRK_TIR', 'Tires', 150),
            array('TRK_OIL', 'Oil', 15),
            array('TRK_SPK', 'Spark Plugs', 6)
        ),
    );


    echo "<h1>三重循环打印</h1>";
    for ($layer = 0; $layer < 3; $layer++) {
        echo 'Layer - 层' . $layer . "<br />";
        for ($row = 0; $row < 3; $row++) {
            for ($colum = 0; $colum < 3; $colum++) {
                echo '|' . $categories[$layer][$row][$colum];
            }
            echo '|<br />';
        }
    }


    echo "<h1>数组排序</h1>";
    $products = array('Tires', 'Oil', 'Spark Plugs');
    sort($products);
    for ($i = 0; $i < 3; $i++) {
        echo $products[$i] . "<br />";
    }

    echo "<h1>asort()和ksort()数组排序</h1>";
    $prices = array('Tires' => 100, 'Oil' => 10, 'Spark Plugs' => 4);

    echo "<h1>asort()数组排序 - 元素值（价格）</h1>";
    asort($prices);

    foreach ($prices as $key => $value) {
        echo $key . "-" . $value . "<br />";
    }

    echo "<h1>ksort()数组排序 - 关键字（说明）</h1>";
    ksort($prices);
    foreach ($prices as $key => $value) {
        echo $key . "-" . $value . "<br />";
    }


    echo "<h1>原始二维数组</h1>";
    $products = array(
        array('TIR', 'Tires', 100),
        array('OIL', 'Oil', 10),
        array('SPK', 'Spark Plugs', 4)
    );


    for ($row = 0; $row < 3; $row++) {
        for ($column = 0; $column < 3; $column++) {
            echo '|' . $products[$row][$column];
        }
        echo '|<br />';
    }

    echo "<h1>array_multisort()数组排序 - 对子数组的第一个元素升序排序</h1>";



    array_multisort($products);

    for ($row = 0; $row < 3; $row++) {
        for ($column = 0; $column < 3; $column++) {
            echo '|' . $products[$row][$column];
        }
        echo '|<br />';
    }


    echo "<h1>用户自定义排序排序 - 对价格升序</h1>";
    function compare($x, $y)
    {
        if ($x[2] == $y[2]) {
            return 0;
        } else if ($x[2] < $y[2]) {
            return -1;
        } else {
            return 1;
        }
    }
    usort($products, 'compare'); //用户自定义排序

    for ($row = 0; $row < 3; $row++) {
        for ($column = 0; $column < 3; $column++) {
            echo '|' . $products[$row][$column];
        }
        echo '|<br />';
    }


    echo "<h1>3.7.3 - 用户自定义排序函数的反序</h1>";

    function reverse_compare($x, $y)
    {
        if ($x[2] == $y[2]) {
            return 0;
        } else if ($x[2] < $y[2]) {
            return 1;
        } else {
            return -1;
        }
    };

    usort($products, 'reverse_compare');
    tda($products);


    echo "<h1>3.8 - 对数组进行重新排序</h1>";
    echo "<h2>3.8.1 - shuffle() 随机排序(bobs_front_page.php)</h2>";
    echo "<h2>3.8.2 - 逆序数组内容 - array_reverse()函数(返回修改后的拷贝)</h2>";

    echo "<h3>使用range(1, 10);创建升序数组元素序列</h3>";
    $numbers = range(1,10);
    oda($numbers);
    $numbers = array_reverse($numbers);
    oda($numbers);
    //降序数组
    $numbers = array();
    for($i=10; $i>0; $i--) {
        array_push($numbers, $i);
    };
    oda($numbers);

    echo "<h1>3.9 - 从文件载入数组 - vieworders2.php</h1>";
    
    echo "<h1>3.10 - 执行其他数组操作</h1>";
    echo "<h2>3.10.1 - end()和prev() 逆序显示数组</h2>";
    $array = array(1, 2, 3);

    $value = end ($array);
    while ($value) {
        echo "$value<br />";
        $value = prev($array);
    };
    
    echo "<h2>3.10.2 - 对数组每一个元素应用函数：array_walk()</h2>";
    function my_print($value) {
        echo "$value<br />";
    }
    array_walk($array, 'my_print');

    function my_multiply(&$value, $key, $factor) {
        $value *= $factor;
        echo "$value<br />";
    }
    array_walk($array, 'my_multiply', 3);

    echo "<h2>3.10.3 - 统计数组元素个数array_count_values()</h2>";
    $array = array(4, 5, 1, 2, 3, 1, 2, 1);
    $ac = array_count_values($array);
    oda($ac);
    echo "1出现了".$ac[1]."次<br />";
    foreach ($ac as $key => $value) {
        echo $key . "出现了" . $value . "次<br />";
    }


    echo "<h2>3.10.4 - 将数组转换成标量变量extract() - 把数组关键字当初变量用，值就是数组关键字的值</h2>";
    $array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3');
    extract($array);
    echo "$key1 $key2 $key3<br />";

    $array = array('key1' => 'value1', 'key2' => 'value2', 'key3' => 'value3');
    extract($array, EXTR_PREFIX_ALL, 'my_prefix');
    echo "$my_prefix_key1 $my_prefix_key2 $my_prefix_key3";































































    echo "<h1>这里是末尾</h1>";
    ?>